<template>
  <div>
    <!-- 两种获取id的方法 -->
    <!-- {{ id }}{{ this.$route.params.id }} -->
    <!-- 第一种￥route第二种通过路由配置props为true然后在传id为String类型直接用即可 -->
    <!-- 员工详情{{ id }}{{ this.$route.params.id }} -->
    <el-card style="margin: 20px">
      <el-tabs v-model="activeName">
        <el-tab-pane label="用户管理" name="first">
          <el-form
            style="width: 40%"
            label-width="100px"
            :model="userInfo"
            :rules="rules"
          >
            <el-form-item label="姓名:" prop="username">
              <el-input v-model="userInfo.username" />
            </el-form-item>
            <el-form-item label="新密码:" prop="password">
              <el-input v-model="userInfo.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="updata">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="配置管理" name="second">
          <UserInfo />
        </el-tab-pane>
        <el-tab-pane label="角色管理" name="third">
          <JobInfo />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
import { getUserDetailById, saveUserDetailById } from '@/api/user'
export default {
  name: 'Default',
  components: {
    UserInfo,
    JobInfo
  },
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      activeName: 'first',
      userInfo: {
        //   专门存放基本信息
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '密码长度6-9位', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await getUserDetailById(this.id)
    // console.log(res)
    // this.userInfo.username = res.username
    // this.userInfo.password = res.password
    this.userInfo = res
  },
  methods: {
    async updata() {
      await saveUserDetailById(this.userInfo)
      this.$message.success('更新成功')
    }
  }
}
</script>

<style scoped lang='scss'></style>
